// Decompiled by Jad v1.5.8g. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://www.kpdus.com/jad.html
// Decompiler options: packimports(3) definits fieldsfirst ansi space safe 
// Source File Name:   OrientationHandle.java

package jones.gui;

import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.geom.Point2D;
import java.awt.geom.RectangularShape;
import java.util.Observable;
import java.util.Observer;
import javax.swing.JPanel;
import javax.swing.event.MouseInputAdapter;

// Referenced classes of package jones.gui:
//            Orientation

public class OrientationHandle extends JPanel
    implements Observer
{

    private Orientation orientation = null;
    private double distance = 0;
    private RectangularShape handleShape = null;

    public OrientationHandle(Orientation orientation, RectangularShape handleShape)
    {
        this.orientation = orientation;
        distance = Point2D.distance(orientation.getX(), orientation.getY(), handleShape.getCenterX(), handleShape.getCenterY());
        this.handleShape = (RectangularShape)handleShape.clone();
        init();
    }

    private void init()
    {
        setOpaque(false);
        setForeground(Color.white);
        setBackground(Color.green);
        setCursor(Cursor.getPredefinedCursor(0));
        setVisible(false);
        orientation.addObserver(((Observer) (this)));
        MouseInputAdapter mouseAdapter = ((MouseInputAdapter) (new MouseInputAdapter() {

            private double offsetX = 0;
            private double offsetY = 0;

            public void mousePressed(MouseEvent e)
            {
                offsetX = (double)e.getX() - handleShape.getCenterX();
                offsetY = (double)e.getY() - handleShape.getCenterY();
            }

            public void mouseDragged(MouseEvent e)
            {
                orientation.setAngle(Math.atan2(((double)e.getY() + offsetY) - orientation.getY(), ((double)e.getX() + offsetX) - orientation.getX()));
            }

        }
));
        addMouseMotionListener(((java.awt.event.MouseMotionListener) (mouseAdapter)));
        addMouseListener(((java.awt.event.MouseListener) (mouseAdapter)));
    }

    public void update(Observable obs, Object obj)
    {
        double centerX = orientation.getX() + Math.cos(orientation.getAngle()) * distance;
        double centerY = orientation.getY() + Math.sin(orientation.getAngle()) * distance;
        synchronized (handleShape)
        {
            handleShape.setFrameFromCenter(centerX, centerY, centerX - handleShape.getWidth() / 2D, centerY - handleShape.getHeight() / 2D);
        }
        repaint();
    }

    public boolean contains(int x, int y)
    {
        return handleShape.contains(x, y);
    }

    public void paintComponent(Graphics g)
    {
        super.paintComponent(g);
        synchronized (handleShape)
        {
            Graphics2D g2d = (Graphics2D)g;
            g2d.setColor(getBackground());
            g2d.fill(((java.awt.Shape) (handleShape)));
            g2d.setColor(getForeground());
            g2d.draw(((java.awt.Shape) (handleShape)));
        }
    }


}
